<?php
/**
 * 列表页基类。
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: App.class.php 7638 2011-06-04 07:17:31Z richie $
 * 
 */
class ThinkAPK_Action_App extends ThinkAPK_Action_Web {

	private $options = array();
	private $grade = '';
	private $sort = array(0=>'g.add_time', 1=>'gc.down_total', 2=>'gc.good');
	private $sortText = array(0=>'最新更新', 1=>'下载最多', 2=>'评分最高');
	private $limit = 10;
	private $appId;

	function index(){
		$this->setOptions();
		if($this->id){
			$cat_info = D('Category')->getCategory($this->id);
			$this->assign('is_app_home', false);
			$title = empty($cat_info['seo_title']) ? $cat_info['cat_name'] : $cat_info['seo_title'];
			//排行
   			$this->assign('tops', D('GoodsCount')->get($this->id, 10));
   			//应用信息
   			$this->assign('catinfo', $cat_info);
   			//排序
   			$this->assign('sortOrder', $this->setSort());
   			//列表
   			$this->assign('app_list', $this->getAppList());
			$total = $this->getAppTotal();
			$pageshow = P($total, $this->limit, '', array('url'=>$this->setUrl(array('page'=>'%page%'))));
			$this->assign('pageshow', $pageshow);
			$this->assign('total', $total);
			unset($total, $pageshow);
			$this->setPosition($this->id);
		//默认页
		}else{
			$cat_info = D('Category')->getCategory($this->appId);
			$this->homeApp();
			$title = empty($cat_info['seo_title']) ? 'Android应用' : $cat_info['seo_title'];
		}
		//应用导航
		$appNav = D('Category')->getAppCategory($this->appId, MODULE_NAME);
		$this->assign('appNav', $appNav);
		//
		$this->assign('cat_id', $this->id);
		$this->assign('options', $this->options);
		//页面属性
		$this->setTitle($title);
		$this->setKeywords($cat_info['keywords']);
		$this->setDescription($cat_info['cat_desc']);
		$this->display();
	}
	
	//默认页数据
	private function homeApp(){
		//最新应用
		$this->assign('new_app_list', D('Goods')->recommendApp($this->appId, 10, array(), 'g.add_time DESC, g.goods_id DESC'));
		//推荐分类
		$cat = D('Category')->getIndexRecommendCat($this->appId);
   		$this->assign('recommend_app', $cat);
   		//日常必备
   		$this->assign('bibei', $this->usualApp($cat));
   		//排行
   		$this->assign('tops', D('GoodsCount')->get($this->appId, 10));
   		$this->assign('is_app_home', true);
	}
	
	//应用的日常应用数据
	private function usualApp($cat){
		if(!$cat) return $cat;
		foreach($cat as $key=>$cats){
			$cat[$key]['url'] = U('thinkapk://'.MODULE_NAME.'/index', array('id'=>$cats['id']));
			$cat[$key]['child'] = D('Goods')->recommendApp($cats['id'], 4, array('new'), 'gc.down_total DESC, g.goods_id DESC');
		}
		return $cat;
	}
	
	//应用列表
	private function getAppList(){
		$order = $this->sort[$this->options['dsc']].' DESC';
		$map = $this->getConditionForFitler();
		$row = D()->table(C('DB_PREFIX').'goods AS g')
			->field('g.goods_id as id, gc.down_total, gc.good, gc.bad, g.goods_name as title, g.short_title, g.add_time,  g.goods_icon as icon, g.recommend, g.short_desc as gdesc, g.goods_brief, g.short_desc')
			->where($map)
			->join(C('DB_PREFIX').'goods_count as gc ON gc.goods_id=g.goods_id')
			->group('g.goods_id')
			->limit($this->limit)
			->page($this->options['page'])
			->order($order)
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] =  $this->formatTime($app['add_time'], true);
				unset($row[$key]['goods_name'], $app);
			}
		}
		unset($map);
		return $row;
	}
	
	//应用统计
	private function getAppTotal(){
		$map = $this->getConditionForFitler();
		$row = D()->table(C('DB_PREFIX').'goods AS g')->where($map)->count();
		return $row;
	}
	
	//取得查询筛选条件所需要的查询条件
	private function getConditionForFitler(){
		$map = ' g.is_delete=0 AND g.is_on_sale=1 AND g.cat_id='.$this->id;
		return $map;
	}
	
	//设置排序
	private function setSort(){
		$row = $this->sort;
		foreach ($row as $k=>$st){
			unset($row[$k]);
			$row[$k]['url'] = $this->setUrl(array('dsc'=>$k));
			$row[$k]['name'] = $this->sortText[$k];
			$row[$k]['sort'] = $st;
		}
		return $row;
	}
	
	//设置参数
	private function setOptions(){
		$this->id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$this->appId = C(strtoupper(MODULE_NAME).'_ID');
		$this->grade = isset($_REQUEST['grade']) ? trim($_REQUEST['grade']) : '';
		$this->options['dsc'] = $this->getParemsValue('dsc', '0');
		$this->options['page'] = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
	}
	
	//取得参数值
	private function getParemsValue($name, $default='0'){
		$value=array();
		preg_match("/$name(.*)-/isU", $this->grade.'-', $value);
		return isset($value[1]) ? $value[1] : $default;
	}
	
	//设置url
	private function setUrl($options=array()){
		$option = array(
			'id' => $this->id,
			'page' => $this->options['page'],
			'bid' => 0,
			'dsc' => $this->options['dsc']
		);
		$option = array_merge($option, $options);
		return U('thinkapk://'.MODULE_NAME.'/index', $option);
	}
}
?>